﻿using System;
using Domain.Dummies;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;
using AutoMapper;
using System.Collections.Generic;
using System.Linq;


namespace Services.Dummies.SchedulingService
{
    public class DummySchedulingService : ISchedulingService
    {
        public DummySchedulingService()
        {
            Mapper.CreateMap<TeacherLineWSScheduleDto, TeacherLineScheduleDto>().
                ForMember(dst => dst.SubjectGuid, opt => opt.MapFrom(src => src.Subject.Id));
        }

        public ScheduleDto GetSchedule(Guid personId)
        {
            return new ScheduleDto();
        }

        public bool UpdateSchedule(Guid personId, ScheduleDto scheduleDto)
        {
            return true;
        }



        public TeacherDayScheduleDto GetTeacherScheduleForADay(SessionDto session, Guid TeacherGuid, DateTime targetday)
        {
            /*
             * Assuming schedule only for Korchomkin - ignore parameter TeacherGuid
             */
             
            TeacherWeekFullDataScheduleDto week = Database.KorchomkinWeekFullDataSchedule;            

            TeacherDayScheduleDto dockday = new TeacherDayScheduleDto();
            List<TeacherLineScheduleDto> docklines = new List<TeacherLineScheduleDto>();
            dockday.TeacherGuid = week.Teacher.Id;
            dockday.RequestedDay = targetday;
            dockday.Lines = docklines;

            IEnumerable<TeacherDayItemWSScheduleDto> query = week.Days.Where(day => day.WeekDay.Equals(targetday.DayOfWeek));
            if (query.Count() != 0 && query.First().Lines != null)
            {
                foreach (TeacherLineWSScheduleDto line in query.First().Lines)
                    docklines.Add(Mapper.Map<TeacherLineWSScheduleDto, TeacherLineScheduleDto>(line));

            }

            return dockday;
        }

        public TeacherWeekScheduleDto GetTeacherScheduleForAWeek(SessionDto session, Guid TeacherGuid, DateTime day_in_a_week)
        {
            /*
             * We assuming that day_in_a_week is any day in week time period,
             * but this day pointing to week it's belongs,
             * which (by subscribing integer times form this week) point to week,
             * begins 10.01.2011 (for test only) */

            TeacherWeekFullDataScheduleDto week = Database.KorchomkinWeekFullDataSchedule;
            
            TeacherWeekScheduleDto dockweek = new TeacherWeekScheduleDto();
            List<TeacherDayItemScheduleDto> dockdays = new List<TeacherDayItemScheduleDto>();
            dockweek.Days = dockdays;
            dockweek.RequestedWeek = week.RequestedWeek;
            dockweek.TeacherGuid = week.Teacher.Id;

            foreach (TeacherDayItemWSScheduleDto day in week.Days)
            {
                TeacherDayItemScheduleDto dockday = new TeacherDayItemScheduleDto();
                dockday.WeekDay = day.WeekDay;

                List<TeacherLineScheduleDto> docklines = new List<TeacherLineScheduleDto>();
                dockday.Lines = docklines;

                if (day.Lines != null)
                    foreach (TeacherLineWSScheduleDto line in day.Lines)
                    {
                        TeacherLineScheduleDto dockline = Mapper.Map<TeacherLineWSScheduleDto, TeacherLineScheduleDto>(line);
                        docklines.Add(dockline);
                    }
                dockdays.Add(dockday);
            }

            return dockweek;
        }
    }
}
